package com.sub.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sub.pojo.Member;
import com.sub.service.IMemberService;
import com.sub.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Submerge.
 * @since 2022-11-04
 */
@RestController
@RequestMapping("/sub/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    @GetMapping("/list")
    public List<Member> memberList(){
        return memberService.list();
    }

    @GetMapping("/page")
    public R memberPage(){
        IPage<Member> iPage= new Page<>(1,10);
        iPage = memberService.page(iPage);
        return R.success(iPage);
    }



    @GetMapping("/findOne/{id}")
    public R<Member> findOneDetail(@PathVariable String id){
        Member byId = memberService.getById(id);
        return R.success(byId);
    }

    @PostMapping("/save")
    public ResponseEntity<Object> saveMember(@RequestBody Member member){
        if (member!=null){
            memberService.save(member);
        }else {
            return ResponseEntity.ok("新增成员失败！");
        }
        return ResponseEntity.ok("新增成员成功！");
    }

    @PutMapping("/update")
    public ResponseEntity updateMember(Member member){
        if (member!=null && StringUtils.isNotBlank(member.getId())){
            memberService.updateById(member);
            return ResponseEntity.ok("修改成功!");
        }
        return ResponseEntity.ok("修改失败！");
    }

    @DeleteMapping("/deleteByIds")
    public ResponseEntity deleteMemberByIds(String ids){
        if (StringUtils.isNotBlank(ids)){
            List<String> strIds = Arrays.asList(ids.split(","));
            boolean b = memberService.removeByIds(strIds);
            if (b){
                return ResponseEntity.ok("删除成功！");
            }else {
                return ResponseEntity.ok("删除失败！");
            }

        }
        return ResponseEntity.ok("ids为空！");
    }



}

